$(function () {
    $('.PNunber .checkbox').click(function (event) {
        if ($('.PNunber .checkbox').hasClass('checked')) {
            $(this).removeClass('checked');
            $('.chp input').addClass('Cn');
        } else {
            $(this).addClass('checked');
            $('.chp input').removeClass('Cn');
        }
    });
    $('input').focusin(function (event) {
        $(this).css('borderColor', '#0079CC');
    });
    $('input').focusout(function (event) {
        $(this).css('borderColor', '#CACACA');
    });
});


window.onload = function () {
    changeRandomCodeImg();
}

layui.use(['layer'], function () {
    var layer = layui.layer;
});

// 注册
$('.Plast .checkbox').click(function (event) {
    if ($('.Plast .checkbox').hasClass('checked')) {
        $(this).removeClass('checked');
        $('.Pbtn a').css('background', '#999');
        $('.Pbtn a').removeAttr('href');
    } else {
        $(this).addClass('checked');
        $('.Pbtn a').css('background', '#0079CC');
        $('.Pbtn a').attr("href", "javascript:doReg();");
    }
});
let regUser = {};

function doReg() {
    let phoneNum = document.querySelector("body > div.bd > div > div > form > div > p:nth-child(2) > input").value;
    let passWord = document.querySelector("body > div.bd > div > div > form > div > p:nth-child(3) > input[type=password]").value;
    let rePassWord = document.querySelector("body > div.bd > div > div > form > div > p:nth-child(4) > input").value;
    let randomCode = document.querySelector("body > div.bd > div > div > form > div > p:nth-child(5) > input[type=text]").value;
    let email = document.querySelector("body > div.bd > div > div > form > div > p:nth-child(7) > input").value;
    if (Bee.PhoneUtils.isPhoneNum(phoneNum)) {
        console.log("手机号码！" + phoneNum);
        regUser.mobile = phoneNum;
    } else {
        layer.msg("请使用规范手机号!")
        return;
    }
    if (passWord.length < 6) {
        layer.msg("密码不能少于六位!")
        return;
    }
    if (passWord != rePassWord) {
        layer.msg("密码与重复密码不一致！")
        return;
    }
    if (Bee.EmailUtils.isEmail(email)) {
        console.log("电子邮箱！" + email);
        regUser.email = email;
    } else {
        layer.msg("请使用邮箱!")
        return;
    }
    //密码与确认密码必须相同，且位数不能少于六位

    regUser.password = passWord;
    regUser.randomCode = randomCode;

    console.log("注册的标识符：" + phoneNum);
    console.log("执行用户注册...");

    $.ajax({
        url: "/Team_Aomaotuan_war/users/reg",
        type: "POST",
        data: JSON.stringify(regUser),
        dataType: "text",
        success: function (resp) {
            let result = JSON.parse(resp);//字符串转成JSON对象

            console.log(resp);
            if (result.code === 200) {
                layer.msg(result.msg);
                //注册成功后，两秒钟自动跳转到登录界面。
                window.setTimeout("javascript:window.location.href = 'login.html'", 2000);
            } else {
                layer.msg(result.msg)
            }
        }
    });
}

function changeRandomCodeImg() {
    //document.querySelector("#randomCodeImg")
    $("#randomCodeImg").prop("src", "WebContent/user/commons/randomCodeImg?tm" + new Date().getMilliseconds())
}

//密码框改变相应事件
$("#inputPassWord").bind('input propertychange', function () {
    const password = document.getElementById("inputPassWord").value;
    console.log(password);
    console.log(checkStrong(password));
    const kind = checkStrong(password);
    for (let i = 0; i < 3; i++) {
        const a = document.getElementsByTagName("strong")[i];
        if (kind === i) {
            a.style.background = "green";
        } else {
            a.style.background = "#333";
        }
    }
});

//检查密码强度
function checkStrong(val) {
    var modes = 0;
    if (val.length < 6) return -1;//不符合规范
    if (/\d/.test(val)) modes = 0; //数字
    if (/[a-zA-Z]/.test(val)) modes++; //字母
    if (/\W/.test(val)) modes++; //特殊字符
    return modes;
};

function openText() {
    layer.confirm(document.querySelector("body > div.bd > div > div > form > div > p.clearfix.Plast > span.agreement").innerHTML
        , {
            area: ['700px', '450px'],
            btn: ['阅读完毕']
        });
}